package ezXpns.GUI.controllers;

import java.util.Date;

import org.apache.commons.lang3.tuple.Pair;

import ezXpns.BudgetChange;
import ezXpns.Profile;
import ezXpns.GUI.ProgressBar;
import ezXpns.analysis.AggregationTask;
import ezXpns.analysis.AggregationResultHandler;
import ezXpns.analysis.transforms.*;
import ezXpns.analysis.aggregators.*;
import ezXpns.storage.Query;
import ezXpns.storage.Storage;
import ezXpns.storage.queries.DateRangeQuery;
import ezXpns.utils.DateTimeUtils;

/**
 * @author A0086931X
 */

//@author A0086931X
public class ProgressController {	
	public ProgressController(Storage<BudgetChange> storage, final Profile profile){
		this.profile = profile;
		this.storage = storage;
	}

	public void connect(final ProgressBar progressBar) {
		this.progressBar = progressBar;
		progressBar.setValue(0,profile.getBudget());
		
		Pair<Date, Date> range = DateTimeUtils.getMonthRange(new Date());
		Query<BudgetChange> query = new DateRangeQuery(range.getLeft(), range.getRight());
		storage.liveQuery(query,
			AggregationTask.create(
				new AmountExtractor(),
				new Sum(),
				new AggregationResultHandler<Float>() {
					@Override
					public void onNewValue(Float value) {
						progressBar.setValue(value, profile.getBudget());
						System.out.println("HEY!!!" + value);
					}
				}
			)
		);	
	}
	
	public void refresh(){
		connect(progressBar);	
	}

	private ProgressBar progressBar;
	private Storage<BudgetChange> storage;
	private Profile profile;
}
